﻿using System;
using System.Linq;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;

namespace iss.sg4210.se18.team6s.jass.BL
{
    public class AdvertiserFacade
    {
        IAdvertiserAccount advertiserRepo = null;
        IAdvertiserAccountTopUpLog advertiserTopUpLogRepo = null;
        public AdvertiserFacade()
        {
            advertiserRepo = DataAccessFactory.Instance.AdvertiserAccount;
            advertiserTopUpLogRepo = DataAccessFactory.Instance.AdvertiserAccountTopUpLog;
        }

        public IQueryable<AdvertiserAccount> FindAllAdvertiserAccounts()
        {
            try
            {
                return advertiserRepo.FindAllAdvertiserAccounts();
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public AdvertiserAccount GetAdvertiserAccount(Guid id)
        {
            try
            {
                return advertiserRepo.GetAdvertiserAccount(id);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public void AddAdvertiserAccount(AdvertiserAccount advertiserAccount)
        {
            try
            {
                advertiserRepo.AddAdvertiserAccount(advertiserAccount);

                AdvertiserAccountTopUpLog topupLog = new AdvertiserAccountTopUpLog();
                topupLog.Amt = advertiserAccount.AccountBalance;
                topupLog.TopUpDate = DateTime.Now;
                topupLog.UserId = advertiserAccount.UserId;

                AddAdvertiserAccountTopUpLog(topupLog);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_SAVE, ex);
            }
        }

        public void UpdateAdvertiserAccount(AdvertiserAccount advertiserAccount)
        {
            try
            {
                advertiserRepo.UpdateAdvertiserAccount(advertiserAccount);

                AdvertiserAccountTopUpLog topupLog = new AdvertiserAccountTopUpLog();
                topupLog.Amt = advertiserAccount.AccountBalance;
                topupLog.TopUpDate = DateTime.Now;
                topupLog.UserId = advertiserAccount.UserId;

                AddAdvertiserAccountTopUpLog(topupLog);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_UPDATE, ex);
            }
        }

        public void DeleteAdvertiserAccount(Guid id)
        {
            try
            {
                advertiserRepo.DeleteAdvertiserAccount(id);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_DELETE, ex);
            }
        }

        public IQueryable<AdvertiserAccountTopUpLog> FindAllAdvertiserAccountTopUpLogs()
        {
            try
            {
                return advertiserTopUpLogRepo.FindAllAdvertiserAccountTopUpLogs();
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public AdvertiserAccountTopUpLog GetAdvertiserAccountTopUpLog(int id)
        {
            try
            {
                return advertiserTopUpLogRepo.GetAdvertiserAccountTopUpLog(id);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_RETRIEVE, ex);
            }
        }

        public void AddAdvertiserAccountTopUpLog(AdvertiserAccountTopUpLog advertiserTopUpLog)
        {
            try
            {
                advertiserTopUpLogRepo.AddAdvertiserAccountTopUpLog(advertiserTopUpLog);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_SAVE, ex);
            }
        }

        public void UpdateAdvertiserAccountTopUpLog(AdvertiserAccountTopUpLog advertiserTopUpLog)
        {
            try
            {
                advertiserTopUpLogRepo.UpdateAdvertiserAccountTopUpLog(advertiserTopUpLog);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_UPDATE, ex);
            }
        }

        public void DeleteAdvertiserAccountTopUpLog(int id)
        {
            try
            {
                advertiserTopUpLogRepo.DeleteAdvertiserAccountTopUpLog(id);
            }
            catch (Exception ex)
            {
                throw new JASSException(JASSConstants.JASS_ERROR_DELETE, ex);
            }
        }
    }
}
